<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="派送人" prop="preSendUser">
        <el-input
          v-model="queryParams.preSendUser"
          placeholder="请输入派送人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分段订单" prop="ledIds">
        <el-input
          v-model="queryParams.ledIds"
          placeholder="请输入分段订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['extend:ledSend:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['extend:ledSend:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ledSendList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="派送单编号" align="center" prop="sendCode" width="120"/>
      <el-table-column label="派送/转运" align="center" prop="sendType">
        <template #default="scope">
          <span>{{ scope.row.sendType === '1' ? '派送' : '转运' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="派送人" align="center" prop="preSendUserDtLabel" />
      <el-table-column label="派送时间" align="center" prop="preSendTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.preSendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="派送保存事件" align="center" prop="sendTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="派送保存状态" align="center" prop="sendStatus" width="100">
        <template #default="scope">
          <span>{{ scope.row.sendStatus === 'Y' ? '已保存' : '未保存' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="涉及的分段订单" align="left" prop="ledIdsCode" width="180"  :show-overflow-tooltip="true"/>
      <el-table-column label="转运出发网点" align="center" prop="transFromOrg" />
      <el-table-column label="转运到达地点" align="center" prop="transToOrg" />
      <el-table-column label="转运出发时间" align="center" prop="transStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.transStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="转运到达时间" align="center" prop="transEndTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.transEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['extend:ledSend:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listLedSend, getLedSend, delLedSend, addLedSend, updateLedSend } from "@/api/extend/ledSend";

export default {
  name: "LedSend",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预派送单表格数据
      ledSendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sendType: null,
        preSendUser: null,
        preSendTime: null,
        sendTime: null,
        sendStatus: null,
        ledIds: null,
        transFromOrg: null,
        transToOrg: null,
        transStartTime: null,
        transEndTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        preSendUser: [
          { required: true, message: "派送人不能为空", trigger: "blur" }
        ],
        preSendTime: [
          { required: true, message: "派送时间不能为空", trigger: "blur" }
        ],
        sendStatus: [
          { required: true, message: "派送保存状态不能为空", trigger: "change" }
        ],
        ledIds: [
          { required: true, message: "涉及的分段订单不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询预派送单列表 */
    getList() {
      this.loading = true;
      listLedSend(this.queryParams).then(response => {
        this.ledSendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sendId: null,
        sendType: null,
        preSendUser: null,
        preSendTime: null,
        sendTime: null,
        sendStatus: null,
        ledIds: null,
        transFromOrg: null,
        transToOrg: null,
        transStartTime: null,
        transEndTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sendId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预派送单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sendId = row.sendId || this.ids
      getLedSend(sendId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预派送单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sendId != null) {
            updateLedSend(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLedSend(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sendIds = row.sendId || this.ids;
      this.$modal.confirm('是否确认删除预派送单编号为"' + sendIds + '"的数据项？').then(function() {
        return delLedSend(sendIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('extend/ledSend/export', {
        ...this.queryParams
      }, `ledSend_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
